﻿using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using XuanFe.Database.Model;

namespace XuanFe.Database.DAL
{
    public class StaffLoginStateDAL
    {
        private const string SQL_INSERT_WS_STAFF_LOGIN_STATE = "insert into WS_STAFF_LOGIN_STATE(SLS_STAFF_ID,SLS_LOGIN_TIME,SLS_COMPUTER_NAME,SLS_IP_ADDRESS,SLS_MAC_ADDRESS,SLS_SESSION_ID) values(@StaffId,@LoginTime,@ComputerName,@IpAddress,@MacAddress,@SessionId)";
        private const string SQL_DELETE_WS_STAFF_LOGIN_STATE = "delete from WS_STAFF_LOGIN_STATE where 1=1";

        private const string SQL_WHERE_STAFF_ID = " and SLS_STAFF_ID=@StaffId ";
        private const string SQL_WHERE_COMPUTER_NAME = " and SLS_COMPUTER_NAME=@ComputerName ";

        private const string PARM_STAFF_ID = "@StaffId";
        private const string PARM_LOGIN_TIME = "@LoginTime";
        private const string PARM_LOGOUT_TIME = "@LogoutTime";
        private const string PARM_COMPUTER_NAME = "@ComputerName";
        private const string PARM_IP_ADDRESS = "@IpAddress";
        private const string PARM_MAC_ADDRESS = "@MacAddress";
        private const string PARM_SESSIOIN_ID = "@SessionId";

        private static StaffLoginStateDAL instance = new StaffLoginStateDAL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static StaffLoginStateDAL Instance
        {
            get
            {
                return instance;
            }
        }
        public int Insert(DbTransaction trans, StaffLoginStateModel model)
        {
            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.StaffId });
            parms.Add(new MySqlParameter(PARM_LOGIN_TIME, MySqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = model.LoginTime });
            parms.Add(new MySqlParameter(PARM_COMPUTER_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.ComputerName });
            parms.Add(new MySqlParameter(PARM_IP_ADDRESS, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.IPAddress });
            parms.Add(new MySqlParameter(PARM_MAC_ADDRESS, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.MACAddress });
            parms.Add(new MySqlParameter(PARM_SESSIOIN_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.SessionId });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_WS_STAFF_LOGIN_STATE, parms.ToArray());
        }
        public int Delete(DbTransaction trans, string staffId, string computerName)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_DELETE_WS_STAFF_LOGIN_STATE);
            sbSQL.Append(SQL_WHERE_STAFF_ID);
            sbSQL.Append(SQL_WHERE_COMPUTER_NAME);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = staffId });
            parms.Add(new MySqlParameter(PARM_COMPUTER_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = computerName });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, sbSQL.ToString(), parms.ToArray());
        }
    }
}
